package org.nanquan.dotjava.view.filter;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class PageManager {
	
	private static PageManager pageManager;
	
	private Map<String, String> pages=new HashMap<String, String>();
	
	private Set<String> blackExe =new HashSet<String>() ; 
	
	private Set<String> whiteExe =new HashSet<String>() ; 
	
	private Set<String> blackList =new HashSet<String>() ; 
	
	private Set<String> whiteList  =new HashSet<String>() ; 
	
	private Set<String> blackFolder  =new HashSet<String>() ; 
	
	private Set<String> whiteFolder   =new HashSet<String>() ; 
	
	private String webSiteName;
	
	private static final String splitPath="/";
	
	private PageManager()
	{
		
	}
	
	private void addDefaultNoExe()
	{
		blackExe.add("jpg");
		blackExe.add("html");
		blackExe.add("htm");
		blackExe.add("gif");
		blackExe.add("bmp");
		blackExe.add("jpe");
		blackExe.add("jpeg");
		blackExe.add("js");
		blackExe.add("jsf");
		blackExe.add("mp3");
		blackExe.add("pdf");
		blackExe.add("shtml");
		blackExe.add("xml");
		blackExe.add("swf");
		blackExe.add("xslt");
		blackExe.add("wav");
		blackExe.add("zip");
		blackExe.add("rm");
		
	}
	
	public Boolean containsPage(String page)
	{
		return pages.containsKey(page);
	}
	
	public void addHtmlPage(String jspPage,String htmlPage)
	{
		pages.put(jspPage, htmlPage);
	}
	
	public String getHtmlPage(String jspPage)
	{
		return pages.get(jspPage);
	}
	public static PageManager getManager()
	{
		if(pageManager==null)
			pageManager=new PageManager();
		return pageManager;
	}
	
	public String appFolderPath(String before,String after)
	{
		if(before.endsWith(splitPath))
		{
			before=before.substring(0, before.length()-1);
		}
		if(after.startsWith(splitPath))
		{
			after=after.substring(1, after.length());
		}
		before=before+splitPath+after;
		return before;
	}
	
	public Boolean isBlackExe(String url)
	{
		if(this.blackExe.size()==0)
		{
			addDefaultNoExe();
		}
		String urlLower=url.toLowerCase();
		for (String exe : blackExe) {
			if(urlLower.endsWith("."+exe))
				return true;
		}
		return false;
	}
	
	public Boolean isWhiteExe(String url)
	{
		String urlLower=url.toLowerCase();
		for (String exe : whiteExe) {
			if(urlLower.endsWith("."+exe))
				return true;
		}
		return false;
	}
	
	public Boolean isWhiteList(String url)
	{
		for (String one : whiteList) {
			if(one.equals(url))
				return true;
			if(url.startsWith(one+"?"))
				return true;
		}
		return false;
	}
	
	public Boolean isBlackList(String url)
	{
		for (String one : blackList) {
			if(one.equals(url))
				return true;
			if(url.startsWith(one+"?"))
				return true;
		}
		return false;
	}
	
	public Boolean isWhiteFolder(String url)
	{
		for (String one : whiteFolder) {
			if(url.startsWith(one.equals(splitPath)?one:one+splitPath))
				return true;
		}
		return false;
	}
	
	public Boolean isBlackFolder(String url)
	{
		for (String one : blackFolder) {
			if(url.startsWith(one.equals(splitPath)?one:one+splitPath))
				return true;
		}
		return false;
	}
	
	public void addBlackExeBySpliString(String spliString)
	{
		String[] strings = spliString.split(",");
		for (String string : strings) {
			this.blackExe.add(string);
		}
	}
	
	public void addWhiteExeBySpliString(String spliString)
	{
		String[] strings = spliString.split(",");
		for (String string : strings) {
			this.whiteExe.add(string);
		}
	}
	
	public void addBlackListBySpliString(String spliString)
	{
		String[] strings = spliString.split(",");
		for (String string : strings) {
			this.blackList.add(string);
		}
	}
	
	public void addWhiteListBySpliString(String spliString)
	{
		String[] strings = spliString.split(",");
		for (String string : strings) {
			this.whiteList.add(string);
		}
	}
	
	public void addWhiteFolderBySpliString(String spliString)
	{
		String[] strings = spliString.split(",");
		for (String string : strings) {
			this.whiteFolder.add(string);
		}
	}
	
	public void addBlackFolderBySpliString(String spliString)
	{
		String[] strings = spliString.split(",");
		for (String string : strings) {
			this.blackFolder.add(string);
		}
	}

	public Set<String> getBlackExe() {
		return blackExe;
	}

	public void setBlackExe(Set<String> blackExe) {
		this.blackExe = blackExe;
	}

	public Set<String> getBlackFolder() {
		return blackFolder;
	}

	public void setBlackFolder(Set<String> blackFolder) {
		this.blackFolder = blackFolder;
	}

	public Set<String> getBlackList() {
		return blackList;
	}

	public void setBlackList(Set<String> blackList) {
		this.blackList = blackList;
	}

	public Map<String, String> getPages() {
		return pages;
	}

	public void setPages(Map<String, String> pages) {
		this.pages = pages;
	}

	public Set<String> getWhiteExe() {
		return whiteExe;
	}

	public void setWhiteExe(Set<String> whiteExe) {
		this.whiteExe = whiteExe;
	}

	public Set<String> getWhiteFolder() {
		return whiteFolder;
	}

	public void setWhiteFolder(Set<String> whiteFolder) {
		this.whiteFolder = whiteFolder;
	}

	public Set<String> getWhiteList() {
		return whiteList;
	}

	public void setWhiteList(Set<String> whiteList) {
		this.whiteList = whiteList;
	}
}
